package com.fick.tags;

/**
 * 显示指定channelid的栏目图片
 * @author 王浩强
 * 2009-08-15
 */
import java.io.IOException;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import com.fick.hibernate.dao.ChannelDAO;
import com.fick.hibernate.model.Channel;

@SuppressWarnings("serial")
public class ChannelCommonTag extends TagSupport {

	private String htmltype = "li";
	private String styleclass = "";
	private int maxnum = 8;
	private String linkpage = "list.jsp";

	@Override
	public int doStartTag() throws JspException {

		ChannelDAO dao = new ChannelDAO();
		@SuppressWarnings("unchecked")
		List<Channel> list = dao.findPageBySql(Channel.class, 1, maxnum, " where isShow = '0' order by showOrder ");// (Channel.class,
																																																								// " where isShow = '0' ");
		StringBuffer sb = new StringBuffer("");
		if (list != null && list.size() > 0) {
			for (Channel t : list) {
				if ("li".equals(htmltype)) {
					sb.append("<li ");
				}
				// else if
				if (!"".equals(styleclass)) {
					sb.append(" class='");
					sb.append(styleclass);
				}
				sb.append("'>");
				sb.append("<a href='");
				String link = t.getUrl() != null ? t.getUrl() : linkpage;
				sb.append(link);
				if(!link.startsWith("http") && !link.startsWith("index.jsp")){
				sb.append("?channelid=");
				sb.append(t.getChannelId());
				}
				sb.append("' title='");
				sb.append(t.getName());
				sb.append("'>");
				sb.append(t.getName());
				sb.append("</a>");
				if ("li".equals(htmltype)) {
					sb.append("</li>");
				}
			}
		}

		JspWriter out = pageContext.getOut();
		try {
			out.println(sb.toString());
			out.flush();
		} catch (IOException e) {
		}

		return SKIP_BODY;
	}

	public String getHtmltype() {
		return htmltype;
	}

	public String getLinkpage() {
		return linkpage;
	}

	public int getMaxnum() {
		return maxnum;
	}

	public String getStyleclass() {
		return styleclass;
	}

	public void setHtmltype(String htmltype) {
		this.htmltype = htmltype;
	}

	public void setLinkpage(String linkpage) {
		this.linkpage = linkpage;
	}

	public void setMaxnum(int maxnum) {
		this.maxnum = maxnum;
	}

	public void setStyleclass(String styleclass) {
		this.styleclass = styleclass;
	}

}
